<?php

class artikelTable extends Doctrine_Table
{
    public function activeArtikel(Doctrine_Query $q = null) {
        if(is_null($q)) {
            $q = Doctrine_Query::create()->from('artikel a');
                  //  ->leftJoin('a.user u')
        }

        $alias = $q->getRootAlias();

        $q->addOrderBy($alias . '.created_at DESC');

        return $q;
    }

    public function getLatestArtikel() {
        $q = $this->activeArtikel();

        return $q->execute();
    }

    public function getAllArtikel(){
         //$q = Doctrine_Query::create()->
        $q = $this->createQuery('a')
                ->leftJoin('a.user u')
               // ->leftJoin('a.setBidangStudi sbs')
                ->addOrderBy('a.created_at DESC');

        return $q->execute();
    }

    public function getArtikelById($id){
        $q = $this->createQuery('a')
                ->leftJoin('a.user u')
                ->where('id = ?', $id)
                ->limit(1);

        return $q->fetchOne();
    }

    public function getArtikelByUser($id_user) {
        $q = $this->createQuery('a')
                ->leftJoin('a.user u')
                ->where('a.id_penulis = ?', $id_user)
                ->addOrderBy('a.created_at DESC');

        return $q->execute();

    }

    
}
